/**
 * @file Driver_ADC.c
 * @brief 使用了连续转换模式，配合DMA，不断读取
 * @author 2bam 
 * @version 1.0
 * @date 2024-12-01
 * @copyright Copyright (c) 2024  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2024-12-01     1.0     2bam     first version
 */


/* Includes -------------------------------------------------------------------*/
#include "Driver_ADC.h"
#include "adc.h"
#include "Driver_Serial.h"
#include "Driver_OLED.h"
#include "font.h"
#include "rtc.h"
#include "main.h"

/* define ---------------------------------------------------------------------*/
#define ADC_CHANNEL_NUM 3
/* typedef --------------------------------------------------------------------*/

/* variables ------------------------------------------------------------------*/
uint32_t adc_value[ADC_CHANNEL_NUM] = {0};    /** 用于存储三个通道的ADC值 */
/* Private function prototypes ------------------------------------------------*/
static void ShowCurTime(void);



/**
 * @brief 启动ADC转换，搭配DMA使用
 * @date 2024-12-01
 * @author 2bam 
 */
void ADCStart(void)
{
    HAL_ADCEx_Calibration_Start(&hadc1);
    HAL_ADC_Start_DMA(&hadc1,adc_value,sizeof(adc_value)/sizeof(uint32_t)); 
}


/**
 * @brief 显示CPU温度和参考电压
 * @date 2024-12-01
 * @author 2bam 
 */
void ShowCpuTemperature(void)
{
    float cpu_temp = 0.0;
    float voltage = 0.0;
    float rotation = 0.0;

    char message[256] = {0};

    cpu_temp = (1.43f - adc_value[0]*3.3f/4095)*1000/4.3f + 25.0;
    voltage = adc_value[1]*3.3f/4095;
    rotation = adc_value[2]*3.3f/4095;
    printf("cpu temp:%.2f\n",cpu_temp);
    printf("voltage ref:%.2f\n",voltage);
    printf("rotation:%.2f\n",rotation);
    

    /** 使用OLED显示 2024-12-01 Sunday 19:20:41 */
    sprintf(message,"cpu temp:%.2f C",cpu_temp);
    OLED_PrintASCIIString(0,0,message,&afont8x6,OLED_COLOR_NORMAL);
    sprintf(message,"voltage ref:%.2fV",voltage);
    OLED_PrintASCIIString(0,8,message,&afont8x6,OLED_COLOR_NORMAL);
    sprintf(message,"rotation:%.2fV",rotation);
    OLED_PrintASCIIString(0,16,message,&afont8x6,OLED_COLOR_NORMAL);
    ShowCurTime();
    OLED_ShowFrame();
}


/** 打印信息 */
static void ShowCurTime(void)
{
    RTC_TimeTypeDef cur_time;
    char mess[100];

    HAL_RTC_GetTime(&hrtc,&cur_time,RTC_FORMAT_BIN);
    sprintf(mess,"cur time:%d,%d,%d",cur_time.Hours,cur_time.Minutes,cur_time.Seconds);
    printf("cur time:%d,%d,%d\n",cur_time.Hours,cur_time.Minutes,cur_time.Seconds);
    OLED_PrintASCIIString(0,24,mess,&afont8x6,OLED_COLOR_NORMAL);
}
